package com.cityfood.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cityfood.mapper.CfProductMapper;
import com.cityfood.pojo.CfCategory;
import com.cityfood.pojo.CfProduct;
import com.cityfood.pojo.CfProductExample;
import com.cityfood.pojo.CfProductExample.Criteria;
import com.cityfood.service.ProductService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@Service
public class ProductServiceImpl implements ProductService {

	@Autowired
	private CfProductMapper productMapper;

	//根据商品类目id查找商品信息
	@Override
	public List<CfProduct> getProductByCategoryId(Integer categroyId) {
		CfProductExample example = new CfProductExample();
		Criteria criteria = example.createCriteria();
		criteria.andCategoryIdEqualTo(categroyId);
		List<CfProduct> productList = productMapper.selectByExample(example);
		return productList;
	}
	
	//根据商品类目id查找商品信息
		@Override
		public PageInfo<CfProduct> getProductByCategoryIdAndPage(Map<String,Object> productMap,Integer pageNum,Integer pageSize) {
			
			//设置分页信息
			PageHelper.startPage(pageNum, pageSize);
			
			CfProductExample example = new CfProductExample();
			Criteria criteria = example.createCriteria();
			
			if (productMap.containsKey("categoryId")) {
				criteria.andCategoryIdEqualTo(Integer.parseInt(productMap.get("categoryId").toString()));
			}
			if (productMap.containsKey("productNo")) {
				criteria.andProductNoEqualTo((String)productMap.get("productNo"));
			}
			
			if (productMap.containsKey("name")) {
				criteria.andNameLike("%"+(String)productMap.get("name")+"%");
			}

			if (productMap.containsKey("isDisplay")) {
				int isDisplay = Integer.parseInt(productMap.get("isDisplay").toString());
				criteria.andIsdisplayEqualTo((byte)isDisplay);
			}
			
			List<CfProduct> productList = productMapper.selectByExample(example);
			
			//5.获取分页信息
			PageInfo<CfProduct> info = new PageInfo<>(productList);
			
			return info;
		}
	
	@Override
	public List<CfProduct> getProductLikeName(String productName) {
		CfProductExample example = new CfProductExample();
		Criteria criteria = example.createCriteria();
		criteria.andNameLike("%"+productName+"%");
		List<CfProduct> productList = productMapper.selectByExample(example);
		return productList;
	}
	
	@Override
	public List<CfProduct> getAllProducts(){
		CfProductExample example = new CfProductExample();
		List<CfProduct> productList = productMapper.selectByExample(example);
		return productList;
	}

	@Override
	public CfProduct getProductByProductId(Integer productId) {
		CfProductExample example = new CfProductExample();
		Criteria criteria = example.createCriteria();
		criteria.andIdEqualTo(productId);
		List<CfProduct> products = productMapper.selectByExample(example);
		if (products.size()>0) {
			CfProduct product = products.get(0);
			return product;
		}
		return null;
	}

	@Override
	public int insertProduct(CfProduct product) {
		product.setCreateTime(new Date());
		product.setUpdateTime(new Date());
		int i = productMapper.insertSelective(product);
		return i;
	}

	@Override
	public int updateProduct(CfProduct product) {
		product.setUpdateTime(new Date());
		int i = productMapper.updateByPrimaryKeySelective(product);
		return i;
	}

	@Override
	public int deleteProduct(CfProduct product) {
		product.setIsdisplay((byte)0);
		product.setUpdateTime(new Date());
		int i = productMapper.updateByPrimaryKeySelective(product);
		return i;
	}
	
}
